﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
using System.Net;

namespace AlgoLogicMessenger
{
    /// <summary>
    /// Interaction logic for LogIn.xaml
    /// </summary>
    public partial class LogIn : Page
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream = default(NetworkStream);
        string readData = null;
        string IP = "";

        Thread thread;
        bool IsThreadRunning = false;
        ReaderWriterLock rwl = new ReaderWriterLock();
        ArrayList userList = new ArrayList();

        public LogIn()
        {
            InitializeComponent();
            IPHostEntry local = Dns.GetHostByName(Dns.GetHostName());
            foreach (IPAddress IP in local.AddressList)
            {
                tbxIP.Text = IP.ToString();
            }
        }

        string GetUserName()
        {
            string str;
            str = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
            return str;
        }

        private void MulticastSend(string mAddress, int port, string message)
        {
            try
            {
                IPAddress GroupAddress = IPAddress.Parse(mAddress);
                int GroupPort = port;
                UdpClient sender = new UdpClient();
                IPEndPoint groupEP = new IPEndPoint(GroupAddress, GroupPort);
                byte[] bytes = Encoding.ASCII.GetBytes(message);
                sender.Send(bytes, bytes.Length, groupEP);
                sender.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                try
                {
                    AlgoLogicMessenger.IP.Address = tbxIP.Text;
                    clientSocket.Connect(tbxIP.Text, 8888);
                    String message = "Hello#" + GetUserName();
                    MulticastSend("224.168.100.2", 1000, message);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("IP Address error", "Connection Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                serverStream = clientSocket.GetStream();

                byte[] outStream = System.Text.Encoding.ASCII.GetBytes("Facilitator:" + "#");
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();

                //Navigate to another page
                this.NavigationService.Navigate(new Uri("NavigationPage.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Server is Not Connected! Please Try Again!");
            }
        }
    }
}